package cn.hhchat.wp.entity;

import cn.hhchat.wp.dto.group.SimpleJoinRelation;
import cn.hhchat.wp.util.DateUtil;
import org.hibernate.annotations.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.util.Date;

/**
 * Created this one by HMH on 2017/10/22.
 */
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "join_relation_entity")
public class JoinRelationEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne
    @Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
    private GroupEntity group;

    @ManyToOne
    @Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
    private UserEntity user;

    private Boolean needSubmit;

    public SimpleJoinRelation toSimpleJoinRelation() {
        SimpleJoinRelation simpleJoinRelation = new SimpleJoinRelation();
        simpleJoinRelation.setIsAdmin(this.isAdmin);
        simpleJoinRelation.setNeedSubmit(this.needSubmit);
        simpleJoinRelation.setJoinTime(DateUtil.formatDate(this.updateTime));
        simpleJoinRelation.setUserId(this.user.getId());
        simpleJoinRelation.setUserName(this.user.getName());
        simpleJoinRelation.setUserGrade(this.user.getGrade().name());
        simpleJoinRelation.setIsOwner(this.getGroup().getOwner().equals(this.getUser()));
        return simpleJoinRelation;
    }

    private Boolean isAdmin;

    private Boolean isActive;

    @CreationTimestamp
    private Date joinTime;

    @UpdateTimestamp
    private Date updateTime;


}
